/*
* @Author: ls-mac
* @Date:   2018-08-30 14:17:03
 * @Last modified by:   ls-mac
 * @Last modified time: 2018-09-11T18:29:29+08:00
*/
import { parse } from 'url';

// mock tableListDataSource
const obj = {
  k1: [
    {
      a: '建发成都',
      b: '50',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {},
    {},
    {},
    {},
    {},
    {},
  ],
  k2: [
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
  ],
  k3: [
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
  ],
  k4: [
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
  ],
  k5: [
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
  ],
  k6: [
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
    {
      a: '建发成都',
      b: '22',
      c: '4页',
      d: '42.183吨',
    },
  ],
};
const arr = [
  'YY201881500',
  'YY201881501',
  'YY201881502',

  'YY201881503',
  'YY201881504',
  'YY201881505',
  'YY201881506',

  'YY201881507',
  'YY201881508',
  'YY201881509',
  'YY201881510',
  'YY201881512',

  'YY201881513',
  'YY201881514',
  'YY201881515',
  'YY201881516',
  'YY201881517',
  'YY201881518',

  'YY201881520',
];

const car = [
  {
    k1: '18HS101-108-101',
    k2: '热轧卷',
    k3: '2.50*1233*c',
    k4: '10/320',
    k5: '5/160',
    k6: '160',
  },
  {
    k1: '18HS101-108-101',
    k2: '热轧卷',
    k3: '2.50*1233*c',
    k4: '10/320',
    k5: '5/160',
    k6: '160',
  },
  {
    k1: '18HS101-108-101',
    k2: '热轧卷',
    k3: '2.50*1233*c',
    k4: '10/320',
    k5: '5/160',
    k6: '160',
  },
  {
    k1: '18HS101-108-101',
    k2: '热轧卷',
    k3: '2.50*1233*c',
    k4: '10/320',
    k5: '5/160',
    k6: '160',
  },
]

let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
  tableListDataSource.push({
    key: i,
    depotName: null,
    contractDetailCode: 'ERPZHT02',
    productName: '热轧卷',
    spec1: '',
    pieces: 2,
    weight: 500,
    pieces2: 2,
    weight2: 500,
    shopSign: null,
    deliveryType: null,
    heightStart: null,
    heightEnd: null,
    contractDetailCodeStart: null,
    contractDetailCodeEnd: null,
    contractDetailId: '1',
    stockId: null,
    spec2: '',
    spec3: '',
    spec4: '',
    spec5: '',
    spec6: '',
    spec7: '',
    spec8: '2.50',
    spec9: '1200',
    depotId: null,
    reserveDate: null,
    reserveTime: null,
    reserveWeight: null,
    consigneeProvince: null,
    consigneeCity: null,
    consigneeCounty: null,
    address: '福建省福州市台江区',
    pickups: null,
    subData: [
      {
        key: i,
        disabled: i % 6 === 0,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description: '这是一段描述',
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 4,
        updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        progress: Math.ceil(Math.random() * 100),
      },
      {
        key: i,
        disabled: i % 6 === 0,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description: '这是一段描述',
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 4,
        updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        progress: Math.ceil(Math.random() * 100),
      },
      {
        key: i,
        disabled: i % 6 === 0,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description: '这是一段描述',
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 4,
        updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        progress: Math.ceil(Math.random() * 100),
      },
      {
        key: i,
        disabled: i % 6 === 0,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description: '这是一段描述',
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 4,
        updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
        progress: Math.ceil(Math.random() * 100),
      },
    ],
  });
}

export function getAppointment(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = [...tableListDataSource];

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.no) {
    dataSource = dataSource.filter(data => data.no.indexOf(params.no) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    resultSet: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postAppointment(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, no, description } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}
export function depotInfoCard(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, no, description } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    default:
      break;
  }

  const result = obj;

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}
export function depotInfoTabs(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, no, description } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    default:
      break;
  }

  const result = {
    list: arr,
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postCar(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, no, description } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        no: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        description,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    default:
      break;
  }

  const result = {
    list: car,
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export default {
  getAppointment,
  postAppointment,
  depotInfoTabs,
  depotInfoCard,
};
